const db = require('../db/index')
// 获取轮播图列表
exports.carouselList = (req, res) => {
  let pageData = req.body.pageData
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from carousel where appid=? ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from carousel where appid=?`
    db.query(sql2, appid, (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 获取小程序轮播图
exports.carouselListAll = (req, res) => {
  let pageData = req.body
  let appid = pageData.appid
  const sql = `select * from carousel where appid=? ORDER BY sort ASC `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      res: res2
    })
  })
}
// 添加
exports.carouselAdd = (req, res) => {
  let data = req.body
  if (data._id) {
    const sql = `update carousel set name=?,remark=?,image=? where _id= ${data._id}`
    db.query(sql, [data.name, data.remark, data.image], (err, res2) => {
      if (err) return res.cc(err)
      console.log(res2)
      res.send({
        messge: '修改成功'
      })
    })
  } else {
    const sql = `select * from carousel where appid=? `
    db.query(sql, data.appid, (err, res2) => {
      if (err) return res.cc(err)
      data.sort = res2.length
      const sql2 = `insert into carousel set ?`
      db.query(sql2, data, (err, res2) => {
        if (err) return res.cc(err)
        res.send({
          messge: '添加成功'
        })
      })
    })
  }


}
// 排序
exports.carouselSort = (req, res) => {
  let data = req.body
  try {
    data.forEach((item, index) => {
      item.sort = index
      const sql = `update carousel set sort= ? where _id = ${item._id}`
      db.query(sql, [item.sort], (err, res2) => {
        if (err) throw Error(err)
      })
    })
    res.send({
      messge: '排序成功'
    })
  } catch (error) {
    return res.cc(error)
  }

}
// 删除
exports.carouselDelete = (req, res) => {
  let data = req.query.id
  let sql = 'delete from carousel where _id=?'
  db.query(sql, data, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      messge: '删除成功'
    })
  })
}


// 获取改色案例轮播图
exports.caseList = (req, res) => {
  let pageData = req.body.pageData
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from caselist where appid=?and type ='0' ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from caselist where appid=? and type ='0'`
    db.query(sql2, appid, (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 小程序获取改色案例轮播图
exports.caseListAll = (req, res) => {
  let pageData = req.body
  let appid = pageData.appid
  const sql = `select * from caselist where appid=? and type ='0' ORDER BY sort ASC `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      res: res2,
    })
  })
}
// 添加改色案例轮播图
exports.caselistAdd = (req, res) => {
  let data = req.body
  data.type = 0
  if (data._id) {
    const sql = `update caselist set name=?,remark=?,image=? where _id= ${data._id}`
    db.query(sql, [data.name, data.remark, data.image], (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        messge: '修改成功'
      })
    })
  } else {
    const sql = `select * from caselist where appid=? `
    db.query(sql, data.appid, (err, res2) => {
      if (err) return res.cc(err)
      data.sort = res2.length
      const sql2 = `insert into caselist set ?`
      db.query(sql2, data, (err, res2) => {
        if (err) return res.cc(err)
        res.send({
          messge: '添加成功'
        })
      })
    })
  }
}
// 删除
exports.caselistDelete = (req, res) => {
  let data = req.query.id
  let sql = 'delete from caselist where _id=?'
  db.query(sql, data, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      messge: '删除成功'
    })
  })
}

// 获取改色图
exports.caseimgList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from caselist where appid=? and type ='1' ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from caselist where appid=? and type ='1'`
    db.query(sql2, appid, (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}

// 添加改色案例图
exports.caselistimgAdd = (req, res) => {
  let data = req.body
  data.type = 1
  if (data._id) {
    const sql = `update caselist set name=?,remark=?,image=? where _id= ${data._id}`
    db.query(sql, [data.name, data.remark, data.image], (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        messge: '修改成功'
      })
    })
  } else {
    const sql = `select * from caselist where appid=? `
    db.query(sql, data.appid, (err, res2) => {
      if (err) return res.cc(err)
      data.sort = res2.length
      const sql2 = `insert into caselist set ?`
      db.query(sql2, data, (err, res2) => {
        if (err) return res.cc(err)
        res.send({
          messge: '添加成功'
        })
      })
    })
  }
}

// 图片排序
exports.caselistSort = (req, res) => {
  let data = req.body

  try {
    data.forEach((item, index) => {
      item.sort = index
      const sql = `update caselist set sort= ? where _id = ${item._id}`
      db.query(sql, [item.sort], (err, res2) => {
        if (err) throw Error(err)
      })
    })
    res.send({
      messge: '排序成功'
    })
  } catch (error) {
    return res.cc(error)
  }

}

// 获取施工视频
exports.bulidList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from buildvideo where appid=? ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from buildvideo where appid=?`
    db.query(sql2, appid, (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}

// 添加施工视频
exports.bulidAdd = (req, res) => {
  let data = req.body

  if (data._id) {
    const sql = `update buildvideo set name=?,remark=?,video=? where _id= ${data._id}`
    db.query(sql, [data.name, data.remark, data.image], (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        messge: '修改成功'
      })
    })
  } else {
    const sql = `select * from buildvideo where appid=? `
    db.query(sql, data.appid, (err, res2) => {
      if (err) return res.cc(err)
      data.sort = res2.length

      const sql2 = `insert into buildvideo set ?`
      db.query(sql2, data, (err, res2) => {
        if (err) return res.cc(err)
        res.send({
          messge: '添加成功'
        })
      })
    })
  }
}
// 施工视频删除
exports.bulidDelete = (req, res) => {
  let data = req.query.id
  let sql = 'delete from buildvideo where _id=?'
  db.query(sql, data, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      messge: '删除成功'
    })
  })
}
// 图片排序
exports.buildSort = (req, res) => {
  let data = req.body
  try {
    data.forEach((item, index) => {
      item.sort = index
      const sql = `update buildvideo set sort= ? where _id = ${item._id}`
      db.query(sql, [item.sort], (err, res2) => {
        if (err) throw Error(err)
      })
    })
    res.send({
      messge: '排序成功'
    })
  } catch (error) {
    return res.cc(error)
  }
}
// 获取产品列表
exports.productList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from productlist where appid=? limit ${size} OFFSET  ${from} `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from productlist where appid=?`
    db.query(sql2, appid, (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 根据产品类型获取列表
exports.productListType = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid

  const sql = `select * from productlist where appid=? and type=? limit ${size} OFFSET  ${from} `
  db.query(sql, [appid, pageData.type], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from productlist where appid=? and type=?`
    db.query(sql2, [appid, pageData.type], (err2, res3) => {
      if (err2) return res.cc(err2)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 根据产品类型获取列表
exports.productListTypeSerah = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid

  const sql = `select * from productlist where appid=? and type=? and name=? limit ${size} OFFSET  ${from} `
  db.query(sql, [appid, pageData.type, pageData.name], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from productlist where appid=? and type=? and name=?`
    db.query(sql2, [appid, pageData.type, pageData.name], (err2, res3) => {
      if (err2) return res.cc(err2)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}



// 添加产品数据
exports.productAdd = (req, res) => {
  let data = req.body
  data.swiper = JSON.stringify(data.swiper)
  data.images = JSON.stringify(data.images)
  const sql = `select * from productlist where appid=? `
  db.query(sql, data.appid, (err, res2) => {
    if (err) return res.cc(err)

    const sql2 = `insert into productlist set ?`
    db.query(sql2, data, (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        messge: '添加成功'
      })
    })
  })
}

// 修改产品数据
exports.productChange = (req, res) => {
  let data = req.body
  data.swiper = JSON.stringify(data.swiper)
  data.images = JSON.stringify(data.images)

  const sql = `update productlist set name= ?,coverImg=?,title=?,swiper=?,images=? where _id = ${data._id}`
  db.query(sql, [data.name, data.coverImg, data.title, data.swiper, data.images], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      messge: '修改成功'
    })
  })
}
// 施工视频删除
exports.productDelete = (req, res) => {
  let data = req.query.id
  let sql = 'delete from productlist where _id=?'
  db.query(sql, data, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      messge: '删除成功'
    })
  })
}


// 获取icons列表
exports.iconsList = (req, res) => {
  let appid = req.query.id
  const sql = `select * from icons where appid=?  `
  db.query(sql, appid, (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      res: res2,
    })
  })
}
// 获取icons列表
exports.iconsListpost = (req, res) => {
  let data = req.body
  const sql = `select * from icons where appid=? and type=? `
  db.query(sql, [data.appid, data.type], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      res: res2,
    })
  })
}
// icons修改
exports.iconsEmpty = (req, res) => {
  let data = req.body
  const sql = `update icons set name=?,selectImg=?,unSelectImg=?,backImg=?,remark=? where _id=?  `
  db.query(sql, [data.name, data.selectImg, data.unSelectImg, data.backImg, data.remark, data._id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      messge: '修改成功'
    })
  })
}
// 获取关于我们的图片列表
exports.abountList = (req, res) => {
  let pageData = req.body.pageData
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from abount_my where appid=?and type =? ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, [appid, pageData.type], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from abount_my where appid=? and type =?`
    db.query(sql2, [appid, pageData.type], (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 小程序获取关于我们的图片列表
exports.abountListAll = (req, res) => {
  let pageData = req.body
  let appid = pageData.appid
  const sql = `select * from abount_my where appid=? and type=? ORDER BY sort ASC `
  db.query(sql, [appid, pageData.type], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      res: res2,
    })
  })
}
// 关于我们添加
exports.abountAdd = (req, res) => {
  let data = req.body
  if (data._id) {
    const sql = `update abount_my set name=?,remark=?,img=? where _id= ${data._id}`
    db.query(sql, [data.name, data.remark, data.img], (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        messge: '修改成功'
      })
    })
  } else {
    const sql = `select * from abount_my where appid=? `
    db.query(sql, data.appid, (err, res2) => {
      if (err) return res.cc(err)
      data.sort = res2.length
      const sql2 = `insert into abount_my set ?`
      db.query(sql2, data, (err, res2) => {
        if (err) return res.cc(err)
        res.send({
          messge: '添加成功'
        })
      })
    })
  }
}
// 关于我们删除
exports.abountDelete = (req, res) => {
  let data = req.query.id
  let sql = 'delete from abount_my where _id=?'
  db.query(sql, data, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      messge: '删除成功'
    })
  })
}

// 关于我们图片排序
exports.abountSort = (req, res) => {
  let data = req.body
  try {
    data.forEach((item, index) => {
      item.sort = index
      const sql = `update abount_my set sort= ? where _id = ${item._id}`
      db.query(sql, [item.sort], (err, res2) => {
        if (err) throw Error(err)
      })
    })
    res.send({
      messge: '排序成功'
    })
  } catch (error) {
    return res.cc(error)
  }
}
// 图片以及文本列表
exports.imgOrTextList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from imgortext where appid=? and type=? limit ${size} OFFSET  ${from} `
  db.query(sql, [appid, pageData.type], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from imgortext where appid=? and type=?`
    db.query(sql2, [appid, pageData.type], (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 清除文本图片或内容
exports.imgOrTextClear = (req, res) => {
  let data = req.body
  const sql = `update imgortext set image='',text='' where _id=?`
  db.query(sql, [data._id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '修改成功',
      type: 1
    })
  })

}
// 修改内容
exports.imgOrTextChange = (req, res) => {
  let data = req.body
  const sql = `update imgortext set ? where _id=?`
  db.query(sql, [data, data._id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '修改成功',
      type: 1
    })
  })

}


// 产品收藏
exports.productCollect = (req, res) => {
  let data = req.body
  const sqlSelect = `select * from product_collect where userId=? and productId=? and appid=?`
  const sql = `insert into product_collect set ?`
  const sqlDelete = `delete from product_collect  where userId=? and productId=? and appid=?`
  const sqlProductDetils = `select * from productlist where _id=? and appid=?`
  const sqlProductUpdata = `update productlist set collectNumber=? where _id=?`
  db.query(sqlSelect, [data.userId, data.productId, data.appid], (err, res3) => {
    if (err) return res.cc(err)
    if (res3.length == 0) {
      db.query(sql, data, (err, res2) => {
        if (err) return res.cc(err)
        db.query(sqlProductDetils, [data.productId, data.appid], (err, res4) => {
          if (err) return res.cc(err)
          if (res4.length > 0) {
            let collectNumber = res4[0].collectNumber + 1
            db.query(sqlProductUpdata, [collectNumber, data.productId], (err, res5) => {
              res.send({
                message: '收藏成功'
              })
            })
          }
        })
      })


    } else {
      db.query(sqlDelete, [data.userId, data.productId, data.appid], (err, res2) => {
        if (err) return res.cc(err)
        db.query(sqlProductDetils, [data.productId, data.appid], (err, res4) => {
          if (err) return res.cc(err)
          if (res4.length > 0) {
            let collectNumber = res4[0].collectNumber - 1
            db.query(sqlProductUpdata, [collectNumber, data.productId], (err, res5) => {
              res.send({
                message: '取消收藏'
              })
            })
          }
        })
      })

    }

  })

}
// 产品详情
exports.productDetisl = (req, res) => {
  let data = req.body
  const sql = `select * from productlist where _id=?`
  const upSql = `update productlist set lookNumber=? where _id=?`
  db.query(sql, [data._id], (err, res2) => {
    if (err) return res.cc(err)
    if (res2.length > 0) {
      let lookNumber = res2[0].lookNumber + 1
      db.query(upSql, [lookNumber, data._id], (err, res3) => {
        if (err) return res.cc(err)
        res.send({
          res: res2[0]
        })
      })

    }
  })
}

// 产品收藏列表
exports.productCollectList = (req, res) => {
  let data = req.body
  const sql = `select * from product_collect where userId=?`
  const productSql = `select * from productlist where _id in(?)`
  db.query(sql, [data.userId], (err, res2) => {
    if (err) return res.cc(err)
    let productId = []
    if (res2.length > 0) {
      productId = res2.map((item) => {
        return item.productId
      })
      db.query(productSql, [productId], (err, res3) => {
        if (err) return res.cc(err)
        res.send({
          res: res3
        })
      })

    } else {
      res.send({
        res: []
      })
    }
  })
}